package com.kofan.shelter.commons.util.handler;

import com.kofan.shelter.commons.util.exception.CommonException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice//在于捕获controller中出现的异常
public class CommonExceptionHandler {

    /**
     * 拦截Exception类的异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(CommonException.class) //指定使用的异常类
    @ResponseBody
    public Map<String, Object> exceptionHandler(CommonException e) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", e.getCode());
        result.put("msg", e.getMessage());
        return result;
    }
}
